package cn.HuanG.user.service.impl;

import cn.HuanG.base.exception.BusinessException;
import cn.HuanG.base.service.impl.BaseServiceImpl;
import cn.HuanG.user.domain.LoginInfo;
import cn.HuanG.user.domain.User;
import cn.HuanG.user.mapper.LoginInfoMapper;
import cn.HuanG.user.service.ILoginInfoService;
import cn.HuanG.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginInfoServiceImpl extends BaseServiceImpl<LoginInfo> implements ILoginInfoService {

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public LoginInfo countByPhone(User user) {
        //根据前端获取的phone去查询数据库
        System.out.println("用户电话"+user.getPhone());
        LoginInfo loginInfo =  loginInfoMapper.countByPhone(user.getPhone());
        System.out.println("登录"+loginInfo);
        if(loginInfo==null){
           throw new BusinessException("小屁娃，快去注册");
       }
       //将前端用户输入的数据加密
        String md5 = MD5Utils.encrypByMd5(user.getPassword() + loginInfo.getSalt());
        if (!md5.equals(loginInfo.getPassword())){
            throw new BusinessException("你个dsb，账号密码错了");
        }
        return loginInfo;
    }
}
